﻿using System;
using System.Collections.Specialized;
using System.Windows.Controls;
using Notoric.Gui.Windows;

namespace Notoric.Gui.Commands
{
    /// <summary>
    /// Implements a command that increments the z-index of selected sections.
    /// </summary>
    /// <remarks>
    /// The command can execute if and only if the active document is a <see cref="PagePanel"/>
    /// and the active <see cref="ViewModel.Elements.Page"/> has some selected sections
    /// (i.e. <see cref="ViewModel.Elements.Page.SelectedSections"/> collection has at least 1 element).
    /// </remarks>
    public class MoveSectionToFrontCommand : RibbonCommand
    {
        #region Constructors

        /// <summary>
        /// Creates a new <see cref="MoveSectionToFrontCommand"/> instance.
        /// </summary>
        /// <param name="mainWindow">
        /// <para>
        /// Type: <see cref="MainWindow"/>
        /// </para>
        /// <para>
        /// A reference to the main application window.
        /// </para>
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="mainWindow"/> is <see langword="null"/>.
        /// </exception>
        public MoveSectionToFrontCommand(MainWindow mainWindow)
            : base(mainWindow)
        {
            mainWindow.DocumentPane.SelectionChanged += new SelectionChangedEventHandler(DocumentPane_SelectionChanged);
        }

        #endregion

        #region ICommand Members

        /// <inheritdoc/>
        public override bool CanExecute(object parameter)
        {
            return activePage != null && activePage.SelectedSections.Count > 0;
        }

        /// <inheritdoc/>
        public override void Execute(object parameter)
        {
            foreach (var page in activePage.SelectedSections)
            {
                var cmd = page.CreateSetZIndexCommand(page.ZIndex + 1);
                cmd.Execute(null);
            }
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Occurs when the selected document has changed.
        /// </summary>
        /// <param name="sender">Object where the event handler is attached</param>
        /// <param name="e">Event data</param>
        private void DocumentPane_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (activePage != null)
            {
                activePage.SelectedSections.CollectionChanged -= SelectedSections_CollectionChanged;
                activePage = null;
            }

            if (ApplicationWindow.DocumentPane.SelectedItem is PagePanel)
            {
                activePage = ((PagePanel)ApplicationWindow.DocumentPane.SelectedItem).Page;
                activePage.SelectedSections.CollectionChanged += new NotifyCollectionChangedEventHandler(SelectedSections_CollectionChanged);
            }

            RaiseCanExecuteChanged();
        }

        /// <summary>
        /// Occurs when the collection of sections selected on the active page has changed.
        /// </summary>
        /// <param name="sender">Object that has sent the event</param>
        /// <param name="e">Event data</param>
        private void SelectedSections_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            RaiseCanExecuteChanged();
        }

        #endregion

        #region Private Fields

        /// <summary>
        /// Holds the active page.
        /// </summary>
        private ViewModel.Elements.Page activePage;

        #endregion
    }
}